PeerWireHandler{ }
messageReceived{
  try {
    final Object msg=e.getMessage();
    if (!(msg instanceof PeerWireMessage))     return;
    if (msg instanceof HandshakeMessage) {
      final HandshakeMessage message=(HandshakeMessage)msg;
      ((PeerWireFrameDecoder)e.getChannel().getPipeline().get("frame-decoder")).setHandshaked(true);
      final Hash hash=new Hash(HashType.SHA1,message.torrentHash);
      final Torrent torrent=controller.findTorrent(hash);
      if (torrent == null) {
        e.getChannel().disconnect();
        return;
      TorrentPeer peer=torrent.getSwarm().findPeer((InetSocketAddress)e.getChannel().getRemoteAddress(),message.peerID);
      if (peer == null) {
        peer=new TorrentPeer(torrent);
        peer.setAddress((InetSocketAddress)e.getChannel().getRemoteAddress());
      peer.setPeerID(message.peerID);
      this.peer=(PeerWireProtocolPeer)peer.getProtocolPeer();
      e.getChannel().getPipeline().get(PeerTrafficShapingHandler.class).setPeer(peer);
      e.getChannel().getPipeline().get(TorrentTrafficShapingHandler.class).setTorrent(torrent);
      peer.resetState();
      this.peer.getStrategy().getPeerStrategy().peerConnected(torrent,peer);
 else     if (msg instanceof HaveMessage) {
 else     if (msg instanceof UnchokeMessage) {
      peer.getTorrentPeer().getState().setRemoteChoked(TorrentPeerChoking.UNCHOKED);
 else     if (msg instanceof ChokeMessage) {
      peer.getTorrentPeer().getState().setRemoteChoked(TorrentPeerChoking.CHOKED);
      peer.getStrategy().getPeerStrategy().choked(peer.getTorrent(),peer.getTorrentPeer());
 else     if (msg instanceof InterestedMessage) {
      peer.getTorrentPeer().getState().setRemoteInterest(TorrentPeerInterest.INTERESTED);
 else     if (msg instanceof NotInterestedMessage) {
 else     if (msg instanceof BitFieldMessage) {
      peer.getTorrentPeer().getPieces().load(((BitFieldMessage)msg).bitSet);
 else     if (msg instanceof RequestMessage) {
      final RequestMessage message=(RequestMessage)msg;
      final TorrentPieceBlock block=piece.getBlock(message.begin,message.length);
      if (peer.getTorrentPeer().getState().hasUploadRequestedBlock()) {
        peer.disconnect();
        return;
 else     if (msg instanceof CancelMessage) {
      final CancelMessage message=(CancelMessage)msg;
 else     if (msg instanceof BlockMessage) {
      if (piece.isDownloaded()) {
        final Hash pieceHash=controller.getStorage().checksum(piece);
        if (!piece.getHash().equals(pieceHash)) {
          piece.getTorrent().getStrategy().getDownloadStrategy().pieceChecksumFailed(piece.getTorrent(),piece,peer.getTorrentPeer());
 else {
          piece.getTorrent().getStrategy().getDownloadStrategy().pieceComplete(piece.getTorrent(),piece,peer.getTorrentPeer());
 else {
 else {
      System.out.println(msg);
  finally {
    ctx.sendUpstream(e);
}
writeRequested{
    if (!(msg instanceof PeerWireMessage))     return;
      e.getFuture().addListener(new ChannelFutureListener(){
        @Override public void operationComplete(        ChannelFuture future) throws Exception {
 else     if (msg instanceof BlockMessage) {
      final BlockMessage message=(BlockMessage)msg;
      final TorrentPiece piece=peer.getTorrent().getPiece(message.pieceIndex);
      final TorrentPieceBlock block=piece.getBlock(message.begin,message.data.remaining());
      peer.getTorrentPeer().getState().setLastUploadedBlockDate(new Date());
      e.getFuture().addListener(new ChannelFutureListener(){
        @Override public void operationComplete(        ChannelFuture future) throws Exception {
          peer.getTorrentPeer().getState().setUploadRequestedBlock(null);
      final RequestMessage message=(RequestMessage)msg;
      final TorrentPiece piece=peer.getTorrent().getPiece(message.pieceIndex);
      final TorrentPieceBlock block=piece.getBlock(message.begin,message.length);
      peer.getTorrentPeer().getState().setDownloadRequestedBlock(block);
      peer.getTorrentPeer().getState().setDownloadRequestedDate(new Date());
      e.getFuture().addListener(new ChannelFutureListener(){
        @Override public void operationComplete(        ChannelFuture future) throws Exception {
          peer.getTorrentPeer().getState().setDownloadRequestedBlock(null);
          peer.getTorrentPeer().getState().setDownloadRequestedDate(null);
}
operationComplete{
  ((PeerWireFrameEncoder)future.getChannel().getPipeline().get("frame-encoder")).setHandshaked(true);
}
operationComplete{
  if (!future.isSuccess())   return;
  peer.getTorrentPeer().getState().setUploadRequestedBlock(null);
  peer.getTorrentPeer().getState().setUploadRequestedDate(null);
}
operationComplete{
  if (!future.isSuccess())   return;
  peer.getTorrentPeer().getState().setDownloadRequestedBlock(null);
  peer.getTorrentPeer().getState().setDownloadRequestedDate(null);
}
